﻿@page "/logs"
@page "/logs/{Action}"

@using Gurux.DLMS.AMI.Client.Pages.Device
@using Gurux.DLMS.AMI.Client.Pages.Schedule
@using Gurux.DLMS.AMI.Client.Pages.User
@using Gurux.DLMS.AMI.Shared.DIs
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Shared.DTOs
@using System.Diagnostics

@attribute [Authorize(Roles = GXRoles.Admin + "," + GXRoles.AgentManager + "," + GXRoles.AgentGroupManager)]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier2 Notifier
@implements IDisposable

<TabControl Active="@Path" SelectionChanged="@SelectionChanged">
    <TabTextTemplate>
        <span class=@context.Icon aria-hidden="true">
            @if (context.PadgeCount != 0)
            {
                <span class="@context.GetPadgeClass()">
                    @context.PadgeCount
                    <span class="visually-hidden">@context.Padge</span>
                </span>
            }
        </span>
        @context.Text
    </TabTextTemplate>
    <ChildContent>
        <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.SystemLogManager)>
            <TabPage Path="Logs/SystemLog"
                     Text=@Properties.Resources.SystemLogs
                     Padge="New system logs"
                     PadgeCount="@_systemLog.GetCount()"
                     PadgeType="@_systemLog.GetTraceLevel()"
                     Icon="oi oi-warning">
                <SystemLog></SystemLog>
            </TabPage>
        </AuthorizeView>
        <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.TaskManager)>
            <TabPage Path="Logs/Task"
                     Text=@Properties.Resources.Tasks
                     Padge="New tasks"
                     PadgeCount="@_task.GetCount()"
                     PadgeType="@_task.GetTraceLevel()"
                     Icon="oi oi-pulse">
                <Tasks></Tasks>
            </TabPage>
        </AuthorizeView>
        <AuthorizeView Roles="Admin">
            <TabPage Path="Logs/RestStatistic"
                     Text=@Properties.Resources.RESTStatistic
                     Padge="New logs"
                     Icon="oi oi-transfer">
                <RestStatistic></RestStatistic>
            </TabPage>
            <TabPage Path="Logs/Performances"
                     Padge="New logs"
                     Text=@Properties.Resources.Performance>
                <Performances></Performances>
            </TabPage>
        </AuthorizeView>
    </ChildContent>
</TabControl>
<br />
@code {
    /// <summary>
    /// User action.
    /// </summary>
    [Parameter]
    public string? Action { get; set; }

    private string? Path { get; set; }

    /// <summary>
    /// System log stamps.
    /// </summary>
    private GXUserStamp _systemLog = new GXUserStamp();

    /// <summary>
    /// Task stamps.
    /// </summary>
    private GXUserStamp _task = new GXUserStamp();

    /// <summary>
    /// User has selected a new tab.
    /// </summary>
    /// <param name="page">Tab page.</param>
    void SelectionChanged(TabPage page)
    {
        Path = page.Path;
    }

    protected override void OnInitialized()
    {
        //Get selected tab from the url.
        Path = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
        _systemLog = Notifier.GetUserStamps(TargetType.SystemLog);
        _task = Notifier.GetUserStamps(TargetType.Task);
        Notifier.On<IEnumerable<GXUserStamp>>(this, nameof(IGXHubEvents.StampUpdate), (stamps) =>
        {
            bool updated = false;
            foreach (var it in stamps)
            {
                if (it.TargetType == TargetType.SystemLog)
                {
                    updated = true;
                }
                else if (it.TargetType == TargetType.Task)
                {
                    updated = true;
                }
            }
            if (updated)
            {
                StateHasChanged();
                StateHasChanged();
            }
        });
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }
}

